package com.ruoyi.web.controller.talent;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.TalentTemplate;
import com.ruoyi.system.domain.TalentTemplateField;
import com.ruoyi.system.service.ITalentTemplateService;

/**
 * 人才库模板管理 控制器
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/talent/template")
public class TalentTemplateController extends BaseController
{
    @Autowired
    private ITalentTemplateService talentTemplateService;

    /**
     * 查询人才库模板列表
     */
    @PreAuthorize("@ss.hasPermi('talent:template:list')")
    @GetMapping("/list")
    public TableDataInfo list(TalentTemplate talentTemplate)
    {
        startPage();
        List<TalentTemplate> list = talentTemplateService.selectTalentTemplateList(talentTemplate);
        return getDataTable(list);
    }

    /**
     * 导出人才库模板列表
     */
    @PreAuthorize("@ss.hasPermi('talent:template:export')")
    @Log(title = "人才库模板管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TalentTemplate talentTemplate)
    {
        List<TalentTemplate> list = talentTemplateService.selectTalentTemplateList(talentTemplate);
        ExcelUtil<TalentTemplate> util = new ExcelUtil<TalentTemplate>(TalentTemplate.class);
        util.exportExcel(response, list, "人才库模板数据");
    }

    /**
     * 获取人才库模板详细信息
     */
    @PreAuthorize("@ss.hasPermi('talent:template:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(talentTemplateService.selectTalentTemplateById(id));
    }

    /**
     * 根据岗位ID获取模板信息
     */
    @PreAuthorize("@ss.hasPermi('talent:template:query')")
    @GetMapping(value = "/post/{postId}")
    public AjaxResult getByPostId(@PathVariable("postId") Long postId)
    {
        return success(talentTemplateService.selectTalentTemplateByPostId(postId));
    }

    /**
     * 新增人才库模板
     */
    @PreAuthorize("@ss.hasPermi('talent:template:add')")
    @Log(title = "人才库模板管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody TalentTemplate talentTemplate)
    {
        talentTemplate.setCreateBy(getUsername());
        return toAjax(talentTemplateService.insertTalentTemplate(talentTemplate));
    }

    /**
     * 修改人才库模板
     */
    @PreAuthorize("@ss.hasPermi('talent:template:edit')")
    @Log(title = "人才库模板管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody TalentTemplate talentTemplate)
    {
        talentTemplate.setUpdateBy(getUsername());
        return toAjax(talentTemplateService.updateTalentTemplate(talentTemplate));
    }

    /**
     * 删除人才库模板
     */
    @PreAuthorize("@ss.hasPermi('talent:template:remove')")
    @Log(title = "人才库模板管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(talentTemplateService.deleteTalentTemplateByIds(ids));
    }

    /**
     * 查询模板字段列表
     */
    @PreAuthorize("@ss.hasPermi('talent:template:field')")
    @GetMapping("/fields/{templateId}")
    public AjaxResult getFields(@PathVariable("templateId") Long templateId)
    {
        List<TalentTemplateField> fields = talentTemplateService.selectFieldsByTemplateId(templateId);
        return success(fields);
    }

    /**
     * 保存模板字段
     */
    @PreAuthorize("@ss.hasPermi('talent:template:field')")
    @Log(title = "人才库模板字段管理", businessType = BusinessType.UPDATE)
    @PostMapping("/fields/{templateId}")
    public AjaxResult saveFields(@PathVariable("templateId") Long templateId, 
                                 @RequestBody List<TalentTemplateField> fields)
    {
        for (TalentTemplateField field : fields)
        {
            field.setCreateBy(getUsername());
            field.setUpdateBy(getUsername());
        }
        return toAjax(talentTemplateService.saveTemplateFields(templateId, fields));
    }

    /**
     * 查询字段列表（分页）
     */
    @PreAuthorize("@ss.hasPermi('talent:template:field')")
    @GetMapping("/{templateId}/fields/list")
    public TableDataInfo getFieldList(@PathVariable("templateId") Long templateId)
    {
        startPage();
        List<TalentTemplateField> list = talentTemplateService.selectFieldsByTemplateId(templateId);
        return getDataTable(list);
    }

    /**
     * 批量保存字段（中文转拼音）
     */
    @PreAuthorize("@ss.hasPermi('talent:template:field')")
    @Log(title = "人才库模板字段管理", businessType = BusinessType.UPDATE)
    @PostMapping("/{templateId}/fields/batch")
    public AjaxResult saveFieldList(@PathVariable("templateId") Long templateId, 
                                    @RequestBody List<TalentTemplateField> fields)
    {
        return toAjax(talentTemplateService.saveFieldListBatch(templateId, fields));
    }

    /**
     * 删除模板字段
     */
    @PreAuthorize("@ss.hasPermi('talent:template:field')")
    @Log(title = "人才库模板字段管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/fields/{templateId}/{fieldId}")
    public AjaxResult deleteField(@PathVariable("templateId") Long templateId, 
                                   @PathVariable("fieldId") Long fieldId)
    {
        return toAjax(talentTemplateService.deleteTemplateField(templateId, fieldId));
    }

    /**
     * 查询动态表数据
     */
    @PreAuthorize("@ss.hasPermi('talent:template:query')")
    @GetMapping("/data/{tableName}")
    public AjaxResult getData(@PathVariable("tableName") String tableName)
    {
        List<Map<String, Object>> data = talentTemplateService.selectDynamicTableData(tableName);
        return success(data);
    }

    /**
     * 保存动态表数据
     */
    @PreAuthorize("@ss.hasPermi('talent:template:edit')")
    @Log(title = "人才库数据维护", businessType = BusinessType.UPDATE)
    @PostMapping("/data/{tableName}")
    public AjaxResult saveData(@PathVariable("tableName") String tableName, 
                               @RequestBody List<Map<String, Object>> dataList)
    {
        return toAjax(talentTemplateService.saveDynamicTableData(tableName, dataList));
    }
}

